package com.word.vacation.sdk;

import android.app.Activity;
import android.content.Context;
import android.util.Base64;
import android.util.Log;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.word.vacation.sdk.ad.WordMaxReward;
import com.word.vacation.sdk.bridge.WordCocosBridge;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class WordSDKManager {

    public static WordSDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final WordSDKManager GAME_APPLICATION = new WordSDKManager();
    }


    public void init(Activity activity) {
        Log.e("WorV", "onCreateonCreate");
        Context mApplication = activity.getApplicationContext();
        WordBusiness.init(mApplication);
        WordCocosBridge.getInstance().setMainGameActivity(activity);
    }

    public void interInit() {
        setHasUserConsent();
        setDoNotSell();

        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(getKey())
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        AppLovinSdk.getInstance(WordBusiness.context).initialize(initConfig, config -> {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
            Log.e("WorV", "onSdkInitialized");
            WordMaxReward.getInstance().loadRewardAd(WordBusiness.context);
        });
    }

    private String getKey() {
        return decrypt(WordConstant.MAX_KEY, WordConstant.KEY_FLAG);
    }

    private void setDoNotSell() {
        AppLovinPrivacySettings.setDoNotSell(false);
    }

    private void setHasUserConsent() {
        AppLovinPrivacySettings.setHasUserConsent(true);
    }


    //    public String encrypt(String plainText, String key) {
//        try {
//            byte[] iv = "3qR6mNHMfvwTGZJy".getBytes();
//
//            byte[] keyBytes = android.util.Base64.decode(key, Base64.DEFAULT);
//            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
//            IvParameterSpec ivSpec = new IvParameterSpec(iv);
//
//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
//
//            byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
//
//            return Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "";
//    }


    public String decrypt(String encryptedText, String key) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key), getIvSpec());
            byte[] encryptedBytes = Base64.decode(encryptedText, Base64.DEFAULT);
            byte[] originalBytes = cipher.doFinal(encryptedBytes);
            return new String(originalBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private IvParameterSpec getIvSpec() {
        byte[] iv = "3qR6mNHMfvwTGZJy".getBytes();
        return new IvParameterSpec(iv);
    }

    private Key getSecretKey(String key) {
        byte[] keyBytes = Base64.decode(key, Base64.DEFAULT);
        return new SecretKeySpec(keyBytes, "AES");
    }
}
